(function(window, undefined) {
	var _ignorCurOpr = window.desktopIsMoving || false;
	
	var App = window.App = function(id, config){
		if ( !(this instanceof window.App) ) {
			return new window.App( id, config );
		}
		this.id = id;
		this.config = config;
		this.init();
		
	};

App.fn = App.prototype = {
    /** */
	init : function () {
		this.context = {};
		this.states = {opening:false,opened:false,loaded:false};
		var app = this;
		this.eShortcut = App.elemSupport.createShortcutElem(this.config.name, this.config.bigIcon);
		$(this.eShortcut).bind("click",	function(){app.open();});
		
	},
	/** */
	onload: function(event, desktop, shortcutHolder, winHolder, taskItemHolder){
		var app = event.target;
		if(app.states.loaded){
			warn("[app][load]App has loaded before."); return;
		}
		if(!desktop instanceof Desktop){
			error("[app][load] desktop is not instance of Desktop:", typeof desktop); return;
		}
		app.states.loaded = true;
		
		app.context.desktop = desktop;
		app.context.shortcutHolder = shortcutHolder;
		app.context.winHolder = winHolder;
		app.context.taskItemHolder = taskItemHolder;
		
		$(app.eShortcut).appendTo(shortcutHolder);
	},
	/** */
	open:function(){
		var app = this;
		if(_ignorCurOpr)return;
		if(app.states.opening){
			return;
		}
		if(app.states.opened){
			app.focus();
			return;
		}
		app.states.opening = true;
		
		app.winElem = App.winSupport.open(app);
		app.winElem.appendTo(app.context.winHolder);
		app.autoWinPosition();
		app.states.opened = true;
		app.states.opening = false;
		
		app.taskbarItem = App.elemSupport.createTaskbarItem(app.context.desktop.id + app.id, app.config.midIcon, app.config.name);
		$(app.taskbarItem).appendTo(app.context.taskItemHolder);
		$(app.taskbarItem).bind("click", function(){
			if(App.elemSupport.isCurrent(app)){
				info("[app][event] click taskbar item 1 ", app);
				app.hide();
			}else{
				info("[app][event] click taskbar item 2 ", app);
				app.show();
			}
		});
		
		$(app).triggerHandler("open");
	},
	autoWinPosition:function(){
		var app = this;
		var $prev = $(app.winElem).prev();
		if($prev.length!=0){
			var prevWinTop = $prev.position().top;
			var prevWinLeft = $prev.position().left;
			var appWinTop = $(app.winElem).position().top;
			var appWinLeft = $(app.winElem).position().left;
			var flag = false;
			if(Math.abs(prevWinTop - appWinTop)<21){
				appWinTop = prevWinTop + 30;
				flag = true;
			}
			if(Math.abs(appWinLeft - prevWinLeft)<21){
				appWinLeft = prevWinLeft - 30;
				flag = true;
			}
			if(flag)App.winSupport.position(app, {left:appWinLeft+"px",top:appWinTop+"px"});
		}
	},
	/** */
	onopen: function(){
		this.focus();
	},
	/** */
	close:function(){
		var app = this;
		$(app.taskbarItem).remove();
		delete app.taskbarItem;
		delete app.win;
		app.states.opened = false;
		App.winSupport.close(app);
	},
	closeFromWin:function(){
		var app = this;
		$(app.taskbarItem).remove();
		delete app.taskbarItem;
		delete app.win;
		app.states.opened = false;
		$(app).triggerHandler("close");
	},
	/** */
	focus:function(){
		var app = this;
		info("[app][focus]", app);
		
		if(app.context.desktop.isFinishturninto()){
			if(app.states.opened)App.winSupport.focus(app);
		}else{
			app.context.desktop.addFinishturnintoCallback("app"+app.id, function(){
				App.winSupport.focus(app);
			});
		}
		$(app).triggerHandler("beforefocus");
		App.elemSupport.focustaskbar(app.taskbarItem);
	},
	focusFromWin:function(){
		var app = this;
		info("[app][focusFromWin]", app);
		
		$(app).triggerHandler("beforefocus");
		App.elemSupport.focustaskbar(app.taskbarItem);
		$(app).triggerHandler("focus");
	},
	
	/** */
	unfocus: function(){
		var app = this;
		App.elemSupport.unfocustaskbar(app.taskbarItem);
		App.winSupport.unfocus(app);
	},
	
	/** */
	minisizeFromWin: function(){
		var app = this;
		info("[app][minisizeFroWin]", app);
		
		App.elemSupport.unfocustaskbar(app.taskbarItem);
		$(app).triggerHandler("minisize");
	},
	
	/** */
	show: function(){
		this.focus();
	},
	/** */
	hide: function(){
		var app = this;
		App.winSupport.hide(app);
		App.elemSupport.unfocustaskbar(app.taskbarItem);
		$(app).triggerHandler("minisize");
	},
	/** */
	position: function(pos){
		//对pos的属性循环设置CSS时，性能差的明显，差别有那么大吗
		$(this.eShortcut).css(pos);
	},
	size: function(sizes){
		var app = this;
		App.winSupport.size(app, sizes);
	},
	/**
	 * 必须在各父元素加载完成之后执行
	 */
	autoPosition: function(){
		App.elemSupport.autoPosition(this);
	}
	
};

App.winSupport = {
	open: function(app){
		var dlg = art.dialog.open(app.config.url,
			{
				title:app.config.name,
				id:app.winId,
				close:function(){app.closeFromWin();},
				onMinisize:function(){app.minisizeFromWin();},
				onfocus:function(){app.focusFromWin();},
				width:app.config.width||"600px",
				height:app.config.height||"400px"
			}
		);
		app._winSupport_dlg = dlg;
		return dlg.DOM.wrap;
	},
	focus: function(app){
		app._winSupport_dlg.show().zIndex().focus();
	},
	unfocus: function(app){
		app._winSupport_dlg.unfocus();
	},
	hide: function(app){
		app._winSupport_dlg.hide();
	},
	close: function(app){
		app._winSupport_dlg.close();
	},
	size: function(app, sizes){
		app._winSupport_dlg.size(sizes.width, sizes.height);
	},
	position: function(app, pos){
		app._winSupport_dlg.position(pos.left, pos.top);
	}
};

App.elemSupport = {
	createShortcutElem: function(name, icon) {
		var sElem = 
			 	  '<li class="shortcut">'
				+ '		<div class="appitem"><img class="appButton_appIconImg" src="'+ icon + '"/> </div>'
				+ '		<span>' + name + '</span>'
				+ '</li>';
		return $(sElem)[0];
	},
	createTaskbarItem: function(elemName, icon, name){
		var tp = '<a name='+elemName+' class="task-item task-item-current">'+
			'<div class="task-item-icon"><img src="'+icon+'"/></div>'+
			'		<div class="task-item-txt">'+name+'</div>'+
			'	</a>';
		return $(tp)[0];
	},
	focustaskbar:function(taskbarItemElem){
		$(taskbarItemElem).addClass("task-item-current");
	},
	unfocustaskbar: function(taskbarItemElem){
		$(taskbarItemElem).removeClass("task-item-current");
	},
	isCurrent: function(app){
		return $(app.taskbarItem).hasClass("task-item-current");
	},
	autoPosition: function(app){
		var $prev = $(app.eShortcut).prev();
		if($prev.length==0 ){
			app.position({left:"35px", top:"80px"});
			return;
		}else{
			var prevTop = $prev.position().top;
			var prevLeft = $prev.position().left;
			var appTop = prevTop + 100;
			var appLeft = prevLeft;
			
			if(appTop + 100 > $(window).height()){
				appTop = 80;
				appLeft += 100;
			}
			app.position({left:appLeft+"px", top:appTop+"px"});
		}
		
	}

};

})(window);